home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / aijournl / 1989_05 / aimath.pas < prev    next >
Pascal/Delphi Source File  |  1988-08-24  |  4KB  |  176 lines

  1. Unit AIMATH;
  2.  
  3. interface
  4.  
  5. uses aiglob;
  6.  
  7.  
  8. Function Mean(ImagData : ImagType2;x1,x2 : word):double;
  9. Function StDev(ImagData : ImagType2;x1,x2:word;tMean:double):double;
  10. Function Mean2(imagdata:imagtype2;x1,x2:word):double;
  11. Function Max(A,B:word):word;
  12. Function Min(A,B:word):word;
  13. Function MaxMinRatio(A,B : double):double;
  14. Function Kurtosis(imagdata : imagtype2;count,mode:word):double;
  15. Function Skewness(imagdata : imagtype2;count,mode:word):double;
  16.  
  17. Implementation
  18.  
  19.  
  20.  
  21. {---------------------------------------------------------------------}
  22.  
  23. Function Max(A,B:word):word;
  24. begin
  25.   If A > B then
  26.     Max := A
  27.   else
  28.     Max := B;
  29. end;
  30.  
  31. Function Min(A,B:word):word;
  32. begin
  33.   If A < B then
  34.     Min := A
  35.   else
  36.     Min := B;
  37. end;
  38.  
  39. Function MaxMinRatio(A,B:double):double;
  40. begin
  41.   If A < B then
  42.     MaxMinRatio := B/A
  43.   else
  44.     MaxMinRatio := A/B;
  45. end; {end function MaxMinRatio}
  46.  
  47. Function mean2(imagdata:Imagtype2;x1,x2:word):double;
  48.  
  49. function addit2(imagdata:imagtype2;x1,x2:word):word;
  50. begin
  51. inline($8b/$5e/<x2/                {mov bx,x2}
  52.        $8b/$fb/                    {mov di,bx}
  53.        $2b/$5e/<x1/                {sub bx,x1}
  54.        $8b/$cb/                    {mov cx,bx}
  55.        $d1/$e7/                    {shl di,1}
  56.        $8b/$83/$74/$ec/            {mov ax,word ptr [bp+di-138ch]}
  57.        $4f/                        {again: dec di}
  58.        $4f/                        {dec di}
  59.        $03/$83/$74/$ec/            {add ax,word ptr [bp+di-138ch]}
  60.        $e2/$f8/                    {loop again}
  61.        $89/$46/$fe);               {mov [bp-02],ax}
  62.   end;
  63.  
  64. function divide(number,i : double):double;
  65. begin
  66. inline($dd/$46/$f0/
  67.        $dc/$76/$e8/
  68.        $dd/$5e/$f8);
  69. end;
  70.  
  71. begin
  72.   mean2 := divide(addit2(imagdata,x1,x2),x2-x1+1);
  73. end;
  74. {-----------------------------------------------------------------}
  75.  
  76. Function Mean(ImagData : ImagType2;x1,x2 : word):double;
  77. Var
  78.    i        : word;
  79.    meantemp : double;
  80. begin
  81.    meantemp := 0;
  82.  
  83.    for i := x1 to x2 do
  84.      MeanTemp := ImagData[i] + MeanTemp;
  85.  
  86.    If (x2-x1+1) <> 0 then
  87.       Mean := MeanTemp/(x2-x1+1)
  88.    else
  89.       Mean := 0;
  90. end;
  91.  
  92. Function MySqrt(NumIn:double):double;
  93. begin
  94. inline($dd/$46/$f0/       {fld}
  95.        $d9/$fa/           {fsqrt}
  96.        $dd/$5e/$f8);      {fstp}
  97. end;
  98.  
  99. Function StDev(ImagData : ImagType2;x1,x2:word;tMean:double):double;
  100. Var
  101.    i      : word;
  102.    StTemp : double;
  103. begin
  104.   StTemp := 0;
  105.   For i := x1 to x2 do
  106.    StTemp := sqr(ImagData[i] - tMean) + StTemp;
  107.   If (x2-x1 > 0) then
  108.     StDev := Sqrt((StTemp)/(x2-x1))
  109.   else
  110.      StDev := 0;
  111. end;
  112.  
  113. Function Kurtosis(imagdata : imagtype2;count,mode:word):double;
  114. Var
  115.    _mean,_st : double;
  116.    sum,temp  : double;
  117.    i         : word;
  118. begin
  119.   _mean := mean(imagdata,1,count);
  120.   _st   := stdev(imagdata,1,count,_mean);
  121.   sum := 0;
  122.   For i := 1 to count do
  123.   begin
  124.     temp := (imagdata[i] - _mean);
  125.     temp := temp*temp*temp*temp;
  126.     sum  := sum + temp;
  127.   end;
  128.   temp := _st*_st*_st*_st;
  129.   If count*temp <> 0 then
  130.      Kurtosis := (sum/(count*temp)) - 3
  131.   else
  132.      Kurtosis := 0;
  133. end;
  134.  
  135. Function Skewness(imagdata : imagtype2;count,mode:word):double;
  136. Var
  137.    _mean,_st : double;
  138.    sum,temp  : double;
  139.    i         : word;
  140.    sum1,sum2,sum3 : double;
  141. begin
  142.   sum1 := 0;
  143.   sum2 := 0;
  144.   sum3 := 0;
  145.   _mean := mean(imagdata,1,count);
  146.   _st   := stdev(imagdata,1,count,_mean);
  147.  
  148.   sum := 0;
  149.  
  150.  For i := 1 to count do
  151.   begin
  152.     temp := (imagdata[i] - _mean);
  153.     temp := temp*temp*temp;
  154.     sum  := sum + temp;
  155.   end;
  156.   temp := _st*_st*_st;
  157.   If count*temp <> 0 then
  158.      Skewness := (sum/(count*temp))
  159.   else
  160.      Skewness := 0;
  161.  
  162.   {
  163.   for i := 1 to count do
  164.   begin
  165.     sum1 := imagdata[i] + sum1;
  166.     sum2 := (sqr(imagdata[i])) + sum2;
  167.     sum3 := sum3 + (imagdata[i]*imagdata[i]*imagdata[i]);
  168.   end;
  169.   sum := ( (sum3 - (3*_mean*sum2) + (3*_mean*_mean*sum1) )/count );
  170.   sum := sum - (_mean*_mean*_mean);
  171.   sum := sum/(_st*_st*_st);
  172.   skewness := sum;     }
  173.  
  174. end;
  175.  
  176. END.